// pages/search/search.js
var utils = require('../../utils/util.js');

function in_array(search, array) {
    for (var i in array) {
        if (array[i]['name'] == search) {
            return true;
        }
    }
    return false;
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ishide: true,
        islist: true, //列表加载更多
        keyword: "",
        currentPage: 1,
        searchRecord: []

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            searchRecord: wx.getStorageSync("recordPost"),
            number: wx.getStorageSync("wholesaleNum")
        })
        that.systemHot();
        that.lll();

    },
    //商品筛选获取关键词
    onBindTap: function (e) {
        var that = this;
        if (e.detail.value.length <= 0) {
            that.setData({
                ishide: true,
            })
        }
        that.setData({
            keyword: e.detail.value
        })

    },
    //商品筛选条件
    onSearchTap: function (e) {
        var that = this;
        if (that.data.keyword.length != 0) {
            that.uniqueSearchRecord11(that.data.keyword);
        }
        that.setData({
            ishide: false,
            currentPage: 1,
            allListData: [],
        })
        that.goodslist(that.data.keyword, that.data.currentPage);
    },

    //热门搜索
    onHotSearchTap: function (e) {
        var that = this;
        that.setData({
            name: e.currentTarget.dataset.name,
            keyword: e.currentTarget.dataset.name
        })
    },

    //数据去重
    uniqueSearchRecord11: function (newRecord) {
        var that = this;
        var n = []; // 构建新数组
        var post = {
            name: newRecord
        }
        n.push(post);
        var recordPost = that.data.searchRecord || [];
        for (var i = 0; i < recordPost.length; i++) {
            if (n.length < 8) {
                //如果当前数组的第i个已经保存进了临时数组，那么跳过，否者吧当前项push到临时数组里面
                if (!in_array(recordPost[i]['name'], n) && recordPost[i].name !== newRecord && newRecord !== '' && newRecord !== null && newRecord !== undefined) {
                    n.push(recordPost[i]);
                }
            }
        }
        that.setData({
            searchRecord: n
        });
        wx.setStorageSync("recordPost", n);
    },
    //热搜
    systemHot: function () {
        var that = this;
        utils.http("/api/v1/common/system", {}, function (res) {
            that.setData({
                hotData: res.datas.hot,
            })
        })
    },
    //所有的商品列表
    goodslist: function (keyword, currentPage) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/goods/goods_list", {
            keyword: keyword,
            page: currentPage
        }, function (res) {
            utils.hideLoading();
            var allpost = that.data.allListData;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas) {
                res.datas[i].number = 0;
                allpost.push(res.datas[i])
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                allListData: allpost,
                ready: res.datas.length == 0 ? 1 : 2,
            })
            that.lll();
        })
    },
    // 清除所有搜索记录
    onDelAllTap: function (e) {
        var that = this;
        that.setData({
            searchRecord: []
        });
        wx.removeStorageSync('recordPost');
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.goodslist(that.data.keyword, page)
        }

    },
    lll: function (e) {
        var that = this;
        if (that.data.allListData != undefined) {
            var allListData = that.data.allListData;
            for (var i in allListData) {
                allListData[i].number = 0;
                for (var k in wx.getStorageSync("wholesaleCar")) {
                    if (wx.getStorageSync("wholesaleCar")[k].id == allListData[i].id) {
                        allListData[i].number = wx.getStorageSync("wholesaleCar")[k].number;
                    }
                }
            }
            that.setData({
                allListData: allListData
            })
        }
    },
    //加数量
    onPrevTap: function (e) {
        var that = this;
        var itemslist = that.data.allListData;
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.number;
        itemslist[index].number = itemslist[index].number + 1;
        that.setData({
            allListData: itemslist
        })
        var shuzi = wx.getStorageSync("wholesaleNum") || 0;
        var number = shuzi + 1
        that.setData({
            number: number
        })
        wx.setStorageSync("wholesaleNum", number);
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            wholesalespec: e.currentTarget.dataset.wholesalespec,
            selectStatus: true, //购物车做删除时候用
            number: 1 //加减数量
        }
        utils.uniqueSearchRecord1(dataInfo);
    },
    //减数量
    onNextTap: function (e) {
        var that = this;
        var itemslist = that.data.allListData;
        var index = e.currentTarget.dataset.index;
        var ind = e.currentTarget.dataset.ind;
        if (itemslist[index].number > 0) {
            itemslist[index].number = itemslist[index].number - 1;
            var nowStorage = wx.getStorageSync("wholesaleCar");
            for (var i in nowStorage) {
                if (nowStorage[i].id == itemslist[index].id) {
                    nowStorage[i].number = nowStorage[i].number - 1;
                    if (nowStorage[i].number == 0) {
                        nowStorage.splice(i, 1);
                    }
                }
            }
            that.setData({
                number: wx.getStorageSync("wholesaleNum") - 1,
                allListData: itemslist
            })
            wx.setStorageSync("wholesaleCar", nowStorage)
            wx.setStorageSync("wholesaleNum", that.data.number);
        } else {
            utils.message("该宝贝宝贝不能减少哦~")
        }

    },
    //进入购物车
    onCartButtonTap: function (e) {
        wx.navigateTo({
            url: '../shcartshopp/shcartshopp',
        })
    },
})